package com.tem.product.service.impl;

import com.api.manage.pojo.product.CategoryProductBean;
import com.api.manage.pojo.product.SysCategoryBean;
import com.api.manage.redis.RedisCacheUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tem.product.entity.Category;
import com.api.manage.pojo.product.CategoryBean;
import com.tem.product.entity.SysCategory;
import com.tem.product.mapper.CategoryMapper;
import com.tem.product.service.ICategoryAttributeService;
import com.tem.product.service.ICategoryProductService;
import com.tem.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统 服务实现类
 * </p>
 *
 * @author 陈辉
 * @since 2023-11-08
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private ICategoryAttributeService categoryAttributeService;
    @Autowired
    private ICategoryProductService categoryProductService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    public List<CategoryBean> treeBeans(Long sysBranchOfficeId,Long systemLevelId){
        return this.treeBeans(sysBranchOfficeId,systemLevelId,true,false,false);
    }
    public List<CategoryBean> treeBeans(Long sysBranchOfficeId,Long systemLevelId,boolean children,boolean products,boolean attributes){
        List<CategoryBean> beans = this.getAllBeans(sysBranchOfficeId,systemLevelId);
        if(beans==null||beans.size()<1)return null;
        if(products){
            CategoryProductBean categoryProductBean=new CategoryProductBean();
            List<Long> ids = new ArrayList<>();
            for(CategoryBean b:beans){
                ids.add(b.getCategoryId());
            }
            categoryProductBean.setCategoryIds(ids);
            List<CategoryProductBean> productBeans = categoryProductService.selectWithProduct(categoryProductBean);
            if(productBeans!=null&&productBeans.size()>0){
                for(CategoryBean b:beans){
                    for(CategoryProductBean c:productBeans){
                        if(b.getCategoryId().equals(c.getCategoryId())){
                            b.getProducts().add(c);
                        }
                    }
                }
            }
        }
        if(attributes){
            //----
        }
        if(children){
            beans = buildTree(beans, 0L);
        }
        return beans;
    }
    public List<CategoryBean> getAllBeans(Long sysBranchOfficeId,Long systemLevelId){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if(sysBranchOfficeId!=null)
            queryWrapper.lambda().eq(Category::getSysBranchOfficeId,sysBranchOfficeId);
        if(systemLevelId!=null)
            queryWrapper.lambda().eq(Category::getSystemLevelId,systemLevelId);
        queryWrapper.orderByDesc("seq");
        List<Category> list = this.list(queryWrapper);
        if(list==null||list.size()<1)return null;
        List<CategoryBean> beans = new ArrayList<>();
        CategoryBean ctb = null;
        for (Category c:list){
            ctb = new CategoryBean();
            BeanUtils.copyProperties(c,ctb);
            ctb.setSystemTypeName(redisCacheUtil.getSystemTypeName(c.getSysBranchOfficeId(),c.getSystemLevelId()));
            beans.add(ctb);
        }
        return beans;
    }
    /**
     * 新的递归方式
     * @param list
     * @param pid
     * @return
     */
    public List<CategoryBean> buildTree(List<CategoryBean> list, Long pid){
        List<CategoryBean> children = list.stream().filter(x -> x.getParentId().equals(pid)).collect(Collectors.toList());
        List<CategoryBean> subclass = list.stream().filter(x -> !x.getParentId().equals(pid)).collect(Collectors.toList());
        if(children.size() > 0){
            children.forEach(x -> {
                if(subclass.size() > 0){
                    buildTree(subclass,x.getCategoryId()).forEach(
                            y -> x.getChildren().add(y)
                    );
                }
            });
        }
        return children;
    }
}
